package com.nulldev.util.web.nTube.pages.ntv;

import com.nulldev.util.data.FinalBypass;
import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.manage.MarkedForRemoval;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;
import com.nulldev.util.web.nTubeExtensions.services.TwitchTV;

@MarkedForRemoval(reason = "nTV is no more.")
public class NTVGetPage extends HttpPage3 {

	protected static final FinalBypass<TwitchTV> ttv = new FinalBypass<TwitchTV>(null);

	public NTVGetPage() {
		super("/ntv/get");
	}

	@SuppressWarnings("resource")
	@Override
	public Response onRequest(final Request request) {
		final UniJsonValue uv = UniJsonValue.object().add("reason", "nTV is no longer available.").add("error", true);
		return new Response(403, uv, uv).setCustomMime("application/json");
//		final Map<String, String> s = URLUtils.parseQuery(URLUtils.parseURL(request.url()));
//		final UniJsonValue jso = UniJsonValue.object();
//		if (!s.containsKey("target") || s.get("target").isEmpty()) {
//			jso.add("error", "No target provided!");
//			return new Response(200, jso).setCustomMime("application/json");
//		}
//		final String target = s.get("target");
//		if (target.equalsIgnoreCase("twitch")) {
//			if (!s.containsKey("channel")) {
//				jso.add("error", "No channel provided!");
//				return new Response(200, jso).setCustomMime("application/json");
//			}
//			if (ttv.isNull()) {
//				ttv.setObject(new TwitchTV());
//			}
//			final String channel = s.get("channel");
//			jso.add("channelName", channel);
//			final boolean streaming = ttv.getObject().isStreaming(channel);
//			jso.add("streaming", streaming);
//			if (streaming) {
//				jso.add("meta", ttv.getObject().accquire(channel).get());
//				try {
//					final ChannelStreamKeys v = ttv.getObject().generateToken(channel); // TwitchTV.generateToken(channel, apiKey);
//					jso.add("clientKey", ttv.getObject().getKey());
//					final ChannelURLRequest r = new ChannelURLRequest(v);
//					r.type = s.containsKey("lowlat") ? RequestType.LOW_LATENCY : RequestType.NORMAL;
//					String url = ttv.getObject().getStreamM3U8(r); // TwitchTV.getStreamM3U8URL(channel, apiKey, v.getA(), v.getB());
//					url = url.replaceAll(Pattern.quote("https://"), "http://");
//					jso.add("url", "/proxyAPI?url=" + Base64.encodeBase64S(url) + "&twhls");
//					if (Arguments.hasArgument("--NTubeServer.exposeIPs")) {
//						jso.add("oldURL", url);
//					}
//				} catch (Exception e) {
//					JVM.errorln(e);
//					jso.add("error", "Failed to download channel data!");
//				}
//			}
//			// TODO: Implement more targets
//		} else if (target.equalsIgnoreCase("dlive")) {
//			if (!s.containsKey("channel")) {
//				jso.add("error", "No channel provided!");
//				return new Response(200, createStream(jso.toString())).setCustomMime("application/json");
//			}
//			// TODO:
//			// Implement nWS - WebSockets (client atm)
//			// Use nWS to connect to m3u8 peer network.
//			// Return data and give a recheck_in value.
//		} else if (target.equalsIgnoreCase("mixer")) {
//			jso.add("error", "Mixer has been shutdown, sorry about that.");
//			return new Response(404, jso).setCustomMime("application/json");
//		} else if (target.equalsIgnoreCase("euronews")) {
//			try {
//				final String i0 = "https:" + UniJsonValue
//						.parse(HttpClient.getString().url("https://www.euronews.com/api/watchlive.json").create().dataThenClose()).getString("url");
//				final UniJsonValue data = UniJsonValue.parse(HttpClient.getString().url(i0).create().dataThenClose());
//				if (!data.contains("primary") && !data.contains("backup")) {
//					throw new Exception("Unable to extract, both items were null!");
//				}
//				jso.add("channelString", "Now watching EuroNews.");
//				jso.add("url", data.getString("primary"));
//				jso.add("urlALT", data.getString("backup"));
//			} catch (Exception e) {
//				JVM.errorln(e);
//				jso.add("error", "Failed to download target data!");
//			}
//		} else {
//			jso.add("error", "Unknown target type.");
//		}
//		return new Response(200, jso).setCustomMime("application/json");
	}

}
